library(trackViewer)
library(org.Hs.eg.db)
library(TxDb.Hsapiens.UCSC.hg19.knownGene)
test_file1 <- 'FBN1.config.txt'
test_file2 <- 'TNXB.config.txt'
gene_df <- read.table(test_file2,sep="\t",header=TRUE,
                      stringsAsFactors = FALSE)

gene_df$geneid <- get(gene_df$gene,org.Hs.egSYMBOL2EG)

test_tr_list <- list()
for(i in unique(gene_df$refseq)) {
  
  sub_gene_df <- gene_df[gene_df$refseq==i,]
  test_tr <- GRanges(sub_gene_df$chrom,
                     IRanges(start=sub_gene_df$start,end=sub_gene_df$end),
                     strand=sub_gene_df$strand,
                     gene=sub_gene_df$geneid,
                     symbol=sub_gene_df$gene,
                     transcript=sub_gene_df$refseq,
                     feature = sub_gene_df$feature)
  
   test_tr_list[[i]]<- new("track", dat=test_tr, type = "transcript", 
                  name = 'gene', style = new("trackStyle", 
                                             color = "lightblue")) 
}

gr <- GRanges(gene_df$chrom[1],
              IRanges(start=min(gene_df$txstart),end=max(gene_df$txend)),
              strand=gene_df$strand[1])

viewerStyle <- trackViewerStyle()
setTrackViewerStyleParam(viewerStyle, "margin", c(.1, .05, .02, .02))
vp <- viewTracks(trackList(test_tr_list), 
                 gr=gr, viewerStyle=viewerStyle, 
                 autoOptimizeStyle=TRUE)


